<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class My_account_c extends CI_Controller {
        
        function __construct() {
            parent::__construct();
            $this->load->model(array('coordinator_m'));
        }
        
        function index($msg = '') {
            $data['content'] = 'pages/my_account';
            $data['title'] = 'My Account';
            $data['current_page'] = 'my-account-page';
            $data['class'] = '';
            $data['back_to'] = 'my_dashboard';
            $data['details'] = $this->coordinator_m->get_my_account($this->session->userdata('accid'));
            $data['msg'] = $msg;
            $this->load->view('includes/template', $data);
        }
        
        function my_account() {
            $status = '';
            $data = '';
            $id = $this->coordinator_m->get_my_account($this->session->userdata('accid'));
            if($id!=FALSE){
                $status = 'success';
                $data = $id;
            }else{
                $status = 'error';
                $data = 'Unable to get your account details. Please try again later.';
            }
            echo json_encode(array('status'=>$status, 'account'=>$data));
        }
        
        function upload_profile_pic(){
            $file_element_name = 'profile-pic-file';
            $status = '';
            $msg = '';
            $image = '';
            $config['upload_path'] = './uploads/'.$this->session->userdata('accid');
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '2048';
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload($file_element_name)){
                $status = 'error';
                $msg = $this->upload->display_errors('', '');
            }else{
                $upload_data = $this->upload->data();
                $data = array('profile_pic' => $upload_data['file_name']);
                $dir = 'uploads/'.$this->session->userdata('accid');
                $leave_files = array($upload_data['file_name']);
                foreach( glob("$dir/*") as $file ) {
                    if( !in_array(basename($file), $leave_files) )
                        unlink($file);
                }
                $id = $this->coordinator_m->update_my_account($this->session->userdata('accid'), $data);
                if($id>0){
                    $status = 'success';
                    $msg = 'Profile picture successfully updated.';
                    $image = '/uploads/'.$this->session->userdata('accid').'/'.$upload_data['file_name'];
                }else{
                     unlink($upload_data['full_path']);
                     $status = "error";
                     $msg = 'Something went wrong when saving the file, please try again.';
                     $image = 'assets/images/default-avatar.png';
                }
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg, 'image'=>$image));
        }
        function update_my_account() {
            $status = '';
            $msg = '';
            $data = array(
                'coord_lname' => $this->input->post('lname'),
                'coord_fname' => $this->input->post('fname'),
                'coord_mname' => $this->input->post('mname'),
                'coord_bday' => $this->input->post('bday'),
                'coord_gender' => $this->input->post('gender'),
                'coord_email' => $this->input->post('email'),
                'coord_address' => $this->input->post('address'),
                'coord_num' => $this->input->post('contact_no')
            );
            $id = $this->session->userdata('accid');
            $result = $this->coordinator_m->update_my_account($id, $data);
            if($result>0){
                $status = 'success';
                $msg = 'Account successfully updated.';
            }else{
                $status = 'error';
                $msg = 'Nothing was updated.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function change_password() {
            $status = '';
            $msg = '';
            $result = $this->coordinator_m->change_password($this->session->userdata('accid'), 
                    $this->input->post('confirm_pass'));
            if($result>0){
                $status = 'success';
                $msg = 'Password successfull changed. Changes will be applied after you logout.';
            }else{
                $status = 'error';
                $msg = 'Something went wrong while changing your password. Please try again later.';
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg));
        }
        
        function check_old_pass() {
            echo $this->coordinator_m->check_old_password($this->session->userdata('accid'),$this->input->post('old_pass'));
        }
    }
    
/* End of file account_c.php */
/* Location: ./application/controllers/account_c.php */